package interface1;

import javax.swing.*;


import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.tree.DefaultMutableTreeNode;

import back_database.DBAccess;
import back_database.Mytable;
import back_database.Select_Means;
import front_shop.replenish;
import front_shop.return_sales;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class Admin extends JFrame {

	public static JTable table;
	public static Vector columnNames = null;
	public static Vector data = null;
	private String sql;

//    ResultSet rs;  // 返回的查询结果

	private JFrame frame;
	private JPanel panelLeft, panelTop, panelDown;
	private JTree tree;// 使用根节点创建树组件

	public JMenuBar menubar = new JMenuBar();// 以下生成菜单组件对象

	public JMenu file = new JMenu("文件");
	public static JMenuItem front = new JMenuItem("转前台"); // 转前台
	public static JMenuItem exit = new JMenuItem("退出"); // 退出

	public JMenu cashInfo = new JMenu("收支"); // 收支信息
	public static JMenuItem cashbalance = new JMenuItem("收支信息"); // 收支信息

	public JMenu purchase = new JMenu("进货/退货");
	public static JMenuItem purchase2 = new JMenuItem("采购"); // 仓库内采购
	public static JMenuItem returnSales = new JMenuItem("退货"); // 超市内退货
	public static JMenuItem buyInfor = new JMenuItem("采购信息查询"); // 仓库进货
	public static JMenuItem refundInfor= new JMenuItem("退货信息查询"); // 超市内退货

	JScrollPane scrollPane; // 创建滚动面板，包裹树（因为树节点展开后可能需要很大的空间来显示，所以需要用一个滚动面板来包裹）
	JScrollPane scrollTable; // 放右侧表

	DefaultMutableTreeNode rootNode; // 创建根节点

	DefaultMutableTreeNode saleNode; // 创建二级节点
	DefaultMutableTreeNode stockNode;
	DefaultMutableTreeNode userNode;
	DefaultMutableTreeNode infoNode;
	DefaultMutableTreeNode goodsNode; // 商品管理

	// 销售管理子节点
	DefaultMutableTreeNode saleInfoNode; // 创建三级节点
	// 库存查询子节点
	DefaultMutableTreeNode stockInfoNode;
	DefaultMutableTreeNode stockMerchandiseNode;
	// 用户管理子节点
	DefaultMutableTreeNode addUserNode;
	// 信息查询子节点
	DefaultMutableTreeNode goodsNameNode;
	DefaultMutableTreeNode goodsIdNode;
	DefaultMutableTreeNode goodsBarCodeNode;
	DefaultMutableTreeNode goodsTypeNode;
	// 商品类别子节点
	DefaultMutableTreeNode electricTypeNode;
	DefaultMutableTreeNode giftTypeNode;
	DefaultMutableTreeNode dailyTypeNode;
	DefaultMutableTreeNode fruitsTypeNode;
	DefaultMutableTreeNode foodTypeNode;
	DefaultMutableTreeNode freshTypeNode;
	// 商品管理子节点
	DefaultMutableTreeNode repositoryNode; // 入库出库
	DefaultMutableTreeNode buygoodsNode; // 进货
	DefaultMutableTreeNode returngoodsNode; // 退货
	DefaultMutableTreeNode newGoodsNode; // 新增货

	DefaultMutableTreeNode returnInfor; // 退货信息查询 --- 超市内退货
	DefaultMutableTreeNode inGoodsInfor; // 进货信息查询 ----仓库进货

	public static DefaultTableModel defaultModel = null;

	public static Select_Means select_meands = null; // 调用它的查询和SQL

	ItemListener itemlistener = new ItemListener();

	public Admin() {
		
		file.add(front);  // 文件的子菜单
		file.addSeparator();  // 分隔线
		file.add(exit);
		
		purchase.add(purchase2);  // 进退货的子菜单
		purchase.add(returnSales);
		purchase.addSeparator();  // 分隔线
		purchase.add(buyInfor);
		purchase.add(refundInfor);
		
		cashInfo.add(cashbalance);

//		columnNames.set

		menubar.add(file);
		menubar.add(purchase);
		menubar.add(cashInfo);

		front.addActionListener(itemlistener); // 前台
		exit.addActionListener(itemlistener); // 退出
		purchase2.addActionListener(itemlistener); // 采购
		returnSales.addActionListener(itemlistener); // 退货
		buyInfor.addActionListener(itemlistener); // 进货信息
		refundInfor.addActionListener(itemlistener); // 退货信息
		cashbalance.addActionListener(itemlistener); // 流水

		panelLeft = new JPanel(); // 左侧目录树
		panelTop = new JPanel();
		panelDown = new JPanel();
//		new DBAccess();
		sql = "select * from merchandise ";
		select_meands = new Select_Means(); // 调用它的查询和SQL

		frame = new JFrame("进销存管理系统");
		frame.setSize(1500, 900);
		frame.setLocationRelativeTo(null);
//		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);	 // 单击关闭按钮时退出程序
	
		frame.setLayout(new BorderLayout());
		frame.setJMenuBar(menubar);
		frame.setVisible(true);
//        frame.add(panelRight, BorderLayout.CENTER);

		rootNode = new DefaultMutableTreeNode(" 进销存管理     ");

		saleNode = new DefaultMutableTreeNode("销售管理"); // 创建二级节点
		stockNode = new DefaultMutableTreeNode("库存管理");
		userNode = new DefaultMutableTreeNode("用户管理");
		infoNode = new DefaultMutableTreeNode("信息管理");
		goodsNode = new DefaultMutableTreeNode("商品管理");

		// 把二级节点作为子节点添加到根节点
		rootNode.add(saleNode);
		rootNode.add(stockNode);
		rootNode.add(userNode);
		rootNode.add(infoNode);
		rootNode.add(goodsNode);

		saleInfoNode = new DefaultMutableTreeNode("销售信息浏览");

		stockInfoNode = new DefaultMutableTreeNode("库存信息浏览");
		stockMerchandiseNode = new DefaultMutableTreeNode("按库存商品编号查询");

		addUserNode = new DefaultMutableTreeNode("添加/修改用户");

		goodsNameNode = new DefaultMutableTreeNode("按商品名称查询");
		goodsIdNode = new DefaultMutableTreeNode("按商品编号查询");
		goodsBarCodeNode = new DefaultMutableTreeNode("按商品条形码查询");
		goodsTypeNode = new DefaultMutableTreeNode("按商品类别查询");

		electricTypeNode = new DefaultMutableTreeNode("电器类");
		giftTypeNode = new DefaultMutableTreeNode("礼品类");
		dailyTypeNode = new DefaultMutableTreeNode("日用类");
		fruitsTypeNode = new DefaultMutableTreeNode("蔬果类");
		foodTypeNode = new DefaultMutableTreeNode("食品类");
		freshTypeNode = new DefaultMutableTreeNode("生鲜类");

		repositoryNode = new DefaultMutableTreeNode("入库/出库");
		buygoodsNode = new DefaultMutableTreeNode("进货");
		returngoodsNode = new DefaultMutableTreeNode("退货");
		newGoodsNode = new DefaultMutableTreeNode("新增商品");
		returnInfor = new DefaultMutableTreeNode("退货信息查询");
		inGoodsInfor = new DefaultMutableTreeNode("进货信息查询");

//       goodsNode.add(repositoryNode); // 入库出库
//       goodsNode.add( newGoodsNode );   // 新增商品
		goodsNode.add(buygoodsNode); // 进货
		goodsNode.add(returngoodsNode); // 退货
		goodsNode.add(returnInfor); // 退货信息查询 --- 超市内退货
		goodsNode.add(inGoodsInfor); // 进货信息查询 ----仓库进货

		// 把三级节点作为子节点添加到相应的二级节点
		// 销售管理
		saleNode.add(saleInfoNode);
		// 库存管理
		stockNode.add(stockInfoNode);
		stockNode.add(stockMerchandiseNode);
		// 用户管理
		userNode.add(addUserNode);
		// 信息查询：
		// 商品类别查询
		goodsTypeNode.add(electricTypeNode);
		goodsTypeNode.add(giftTypeNode);
		goodsTypeNode.add(dailyTypeNode);
		goodsTypeNode.add(fruitsTypeNode);
		goodsTypeNode.add(foodTypeNode);
		goodsTypeNode.add(freshTypeNode);
		// 信息查询：
		infoNode.add(goodsNameNode);
		infoNode.add(goodsIdNode);
		infoNode.add(goodsBarCodeNode);
		infoNode.add(goodsTypeNode);

		tree = new JTree(rootNode);// 使用根节点创建树组件

		tree.setRowHeight(45); // 间距
		tree.setShowsRootHandles(true); // 设置树显示根节点句柄
		tree.setEditable(false); // 设置树节点可编辑

//       Vector columnNames = createColumnNames();
//       Vector data = new Result().getTable(sql);
//       Vector data = new Select_Means().Select_shangpin();
//       Vector columnNames = null;
//       Vector data = null;

		defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
		table = new JTable(defaultModel);

		table.setFont(new Font("微软雅黑", Font.PLAIN, 20)); // 表格内容的字体
		this.table.setRowHeight(30); // 每行高度

		JTableHeader tableHeader = this.table.getTableHeader(); // JTableHeader tableHeader columnNames

		tableHeader.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tableHeader.setPreferredSize(new Dimension(tableHeader.getWidth(), 30));

		scrollPane = new JScrollPane(tree); // 创建滚动面板，包裹树（因为树节点展开后可能需要很大的空间来显示，所以需要用一个滚动面板来包裹）
		scrollTable = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); // 垂直滚动条
//       scrollTable.getViewport().add(table);

		panelLeft.add(scrollPane); // 添加滚动面板到那内容面板

		panelTop.setBackground(Color.pink);
		JButton UpdateInfo = new JButton("修改信息");
		UpdateInfo.setVisible(false);
		UpdateInfo.setEnabled(false);
		panelTop.add(UpdateInfo);
		panelTop.setVisible(false);
		frame.add(panelTop, BorderLayout.NORTH); // 添加到了下边
		frame.add(panelLeft, BorderLayout.WEST);
		frame.add(scrollTable, BorderLayout.CENTER);

		scrollTable.repaint();
		scrollTable.invalidate();
		scrollTable.validate();
		frame.repaint();

		/**
		 * 点击目录树所有事件驱动
		 */
		tree.addTreeSelectionListener(new TreeSelectionListener() { // 设置节点选中监听器
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				panelTop.setVisible(false);
				UpdateInfo.setEnabled(false);
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

				// goodsNameNode
				if (node.getUserObject() == goodsNameNode.getUserObject()) {
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = new Select_Means().Select_shangpin();
					defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
					table.setModel(defaultModel);

					String inputValue = JOptionPane.showInputDialog(new String("商品名称查询"));
					if (inputValue == null) {
						return;
					} // 取消按钮 直接 return

					data = select_meands.Select_MerchandiseName(inputValue.trim()); // 查询后，数据变， 表头不变
					defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == goodsIdNode.getUserObject()) {
					// goodsIdNode 按商品编号查询
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = new Select_Means().Select_shangpin();
					defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
					table.setModel(defaultModel);

					String inputValue = JOptionPane.showInputDialog(new String("商品编号查询"));
					if (inputValue == null) {
						return;
					} // 取消按钮 直接 return

					// columnNames = createColumnNames(select_meands.select_shangpin);
					System.out.println(inputValue);
					data = select_meands.Select_sort3(inputValue.trim());
					defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == goodsBarCodeNode.getUserObject()) {
					// 商品条形码查询
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = new Select_Means().Select_shangpin();
					defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
					table.setModel(defaultModel);

					String inputValue = JOptionPane.showInputDialog(new String("商品条形码查询"));
					if (inputValue == null) {
						return;
					} // 取消按钮 直接 return

					System.out.println(inputValue);
					data = select_meands.Select_sort3(inputValue.trim());
					defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
					table.setModel(defaultModel);

				}

				// stockInfoNode 库存信息查询
				if (node.getUserObject() == stockInfoNode.getUserObject()) {
					columnNames = createColumnNames(select_meands.SELECTSTOCK);
					data = select_meands.Select_All_repertory();
					defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == stockMerchandiseNode.getUserObject()) {
					// stockInfoNode 库存编号信息查询
					columnNames = createColumnNames(select_meands.SELECTSTOCK);
					data = select_meands.Select_All_repertory();
					defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
					table.setModel(defaultModel);

					String inputValue = JOptionPane.showInputDialog(new String("库存商品编号查询"));
					if (inputValue == null) {
						return;
					} // 取消按钮 直接 return
					System.out.println(inputValue);
					data = select_meands.Select_RepertoryId(inputValue.trim()); // 查询后，数据变， 表头不变
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);
//    	       scrollTable.getViewport().add(table);
				}

				/*
				 * if(node.getUserObject() == goodsIdNode.getUserObject()) { // 商品编号查询 其实什么都可以查询
				 * String inputValue = JOptionPane.showInputDialog(new String("商品编号查询")); Vector
				 * columnNames = createColumnNames(select_meands.select_shangpin);
				 * System.out.println(inputValue); Vector data =
				 * select_meands.Select_sort3(inputValue.trim());
				 * 
				 * defaultModel = new DefaultTableModel(data, columnNames);// 创建一个默认的表格模型
				 * table.setModel(defaultModel);
				 * 
				 * System.out.println(11); }
				 */

				// 商品类别查询6个
				if (node.getUserObject() == electricTypeNode.getUserObject()) { // electricTypeNode
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = select_meands.Select_sortType(new String("1001")); // DQ 电器--> SortNumber 10001
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == giftTypeNode.getUserObject()) { // giftTypeNode
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = select_meands.Select_sortType(new String("1002")); // LP 礼品--> SortNumber 10002
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == dailyTypeNode.getUserObject()) { // dailyTypeNode
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = select_meands.Select_sortType(new String("1003"));
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == fruitsTypeNode.getUserObject()) { // fruitsTypeNode
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = select_meands.Select_sortType(new String("1004"));
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == foodTypeNode.getUserObject()) { // foodTypeNode
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = select_meands.Select_sortType(new String("1005"));
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == freshTypeNode.getUserObject()) { // freshTypeNode
					columnNames = createColumnNames(select_meands.SELECT_SHANGPIN);
					data = select_meands.Select_sortType(new String("1006"));
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

				}

				if (node.getUserObject() == saleInfoNode.getUserObject()) { // saleInfoNode 销售信息浏览
					columnNames = createColumnNames(select_meands.SELECT_SALESINFO);
					data = select_meands.Select_SalesInfo();
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);
				}
				if (node.getUserObject() == buygoodsNode.getUserObject()) {
					// 进货
					replenish user = new replenish();
					user.setTitle("进货 ");
					user.setBounds(200, 200, 500, 400);
					user.setVisible(true);
					user.validate();
					user.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

				} else if (node.getUserObject() == returngoodsNode.getUserObject()) {
					// 退货 pdf page 14 进入 进退货界面
					return_sales user = new return_sales();
					user.setTitle("退货 ");
					user.setBounds(200, 200, 500, 170);
					user.setVisible(true);
					user.validate();
					user.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

				} 
				else if (node.getUserObject() == returnInfor.getUserObject()) { // 退货信息查询
					columnNames = createColumnNames(select_meands.SELECT_REFUND); // 2021 6 4
					data = new Select_Means().selectRefund();
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

				} else if (node.getUserObject() == inGoodsInfor.getUserObject()) { // 进货信息查询
					columnNames = createColumnNames(select_meands.SELECT_PURCHASE); // 2021 6 4
					data = new Select_Means().selectPurchase();
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

				}


				if (node.getUserObject() == addUserNode.getUserObject()) { // addUserNode
					columnNames = createColumnNames(select_meands.SELECT_USER);
					data = select_meands.select_User(); // DQ 电器--> SortNumber 10001
					defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
					table.setModel(defaultModel);

					UpdateInfo.setVisible(true);
					UpdateInfo.setEnabled(true);
					panelTop.setVisible(true);

					frame.validate();

					// 修改会员密码监听事件
					UpdateInfo.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							String inputNumber = JOptionPane.showInputDialog(new String("请输入要修改的会员号："));
							if (inputNumber == null) {
								System.out.println("取消");
								return;
							} // 取消按钮 直接 return

							System.out.println(inputNumber);
							String selectNumber = select_meands.Select_UserCode(inputNumber); // 记录查询的的密码
							System.out.println(selectNumber);
							// 当 输入的会会员号不存在
							if (selectNumber == "0" || selectNumber == null)
								JOptionPane.showMessageDialog(null, new String("输入的会员账号错误，拒绝修改"));
							else {
								// 存在该账号 判断对应的密码 正确的话， 继续输入密码，修改表格数据 ； 反之 ， 输入错误，从新点击按钮。

								String inputOldCode = JOptionPane.showInputDialog(new String("请输入修改前的密码："));
								if (inputOldCode == null) {
									return;
								} // 取消按钮 直接 return
								System.out.println("输入修改前的密码" + inputOldCode);
								if (inputOldCode.equals(selectNumber)) { // 当被修改的密码 同 输入的密码
									String inputNewCode = JOptionPane.showInputDialog(new String("请输入修改后的密码："));
									if (inputNewCode == null) {
										return;
									} // 取消按钮 直接 return

									select_meands.Select_UserCode(inputNumber, inputNewCode);

								} else {
									System.out.println(inputOldCode + " " + select_meands.Select_UserCode(inputNumber));
									JOptionPane.showMessageDialog(null, new String("输入的密码错误，拒绝修改"));

								}
							}
						}
					});

//    	       data = select_meands.Select_RepertoryId(inputValue.trim());  // 查询后，数据变， 表头不变
//    	       defaultModel = new DefaultTableModel(data, columnNames); // 创建一个默认的表格模型
//    	       table.setModel(defaultModel);

				}

				scrollTable.repaint();
				scrollTable.invalidate();
				scrollTable.validate();
				frame.repaint();
				System.out.println("当前被选中的节点: " + e.getPath());

			}

		});

	}

	/**
	 * createColumnNames 形参 sql语句 返回该sql语句表头信息 返回类型 Vector
	 **/

	public static Vector createColumnNames(String query) {
//		private ResultSetMetaData rsMetaData;
		ResultSetMetaData rsMetaData;
		Vector columnNames = null;
		try {
			rsMetaData = new Result().getRsMetaData(query);

			int columnCount = rsMetaData.getColumnCount();// 字段数目
			columnNames = new Vector<String>();
			for (int i = 1; i <= columnCount; i++) {
				columnNames.add(rsMetaData.getColumnName(i));
			}
		} catch (SQLException e) {
			System.out.println("请输入正确的表名" + e);
		}
		/*
		 * 一开始用的一个一个添加。，后改为Result().getRsMetaData columnNames.add("SequenceNumber");
		 * columnNames.add("MerchandiseNumber"); columnNames.add("TreatyCode");
		 * columnNames.add("SortNumber"); columnNames.add("MerchandiseName");
		 * columnNames.add("RetailPrice"); columnNames.add("Units");
		 * columnNames.add("PurchasePrice");
		 */

		return  columnNames;

	}
	
	  public static void main(String[] args) {
	  
		  Admin admin = new Admin();
	  
	  }
	 

}
